package com.csun.cmny.provider.controller.admin;

import com.alibaba.fastjson.JSON;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.device.csun.sleep.SleepMessageGateway;
import com.csun.cmny.provider.model.dto.csunsleep.CsunSleepDataHiQueryDto;
import com.csun.cmny.provider.model.dto.device.DeviceBindDto;
import com.csun.cmny.provider.model.enums.csunsleep.DmcCsunSleepBindTypeEnum;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.csunsleep.CsunSleepRealDataVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcCsunSleepDataHiService;
import com.csun.cmny.provider.service.DmcCsunSleepDataService;
import com.csun.cmny.provider.utils.DeviceUtils;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-02-15 11:13
 * @Description: the class dmc csun sleep data controller
 */
@Slf4j
@RestController
@RequestMapping(value = "/csunSleep", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcCsunSleepDataController extends BaseController {

    @Resource
    private DmcCsunSleepDataService dmcCsunSleepDataService;

    @Resource
    private DmcCsunSleepDataHiService dmcCsunSleepDataHiService;

    @Resource
    private DeviceUtils deviceUtils;

    @Resource
    private SleepMessageGateway sleepMessageGateway;

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    /**
     * 查询智能床垫实时数据 -- 日创
     *
     * @param deviceId  device id
     *
     * @return  the wrapper.
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/queryCsunSleepRealDataByDeviceId/{deviceId}", method = RequestMethod.POST)
    public Wrapper queryCsunSleepRealDataByDeviceId(@PathVariable Long deviceId) {

        CsunSleepRealDataVo csunSleepRealDataVo = dmcCsunSleepDataService
                .queryCsunSleepRealDataByDeviceId(deviceId);
        if (PublicUtil.isNotEmpty(csunSleepRealDataVo)) {
            DeviceBindDto deviceBindDto = new DeviceBindDto();
            BeanUtils.copyProperties(csunSleepRealDataVo, deviceBindDto);
            OldmanBedRoomBuildingVo obrvo = deviceUtils.getOldmanBedRoomBuildingVo(deviceBindDto);
            if (PublicUtil.isNotEmpty(obrvo)) {
                if (csunSleepRealDataVo.getBindType().equals(DmcCsunSleepBindTypeEnum.OLDMAN.getKey())) {
                    csunSleepRealDataVo.setOldmanName(obrvo.getOldmanName());
                    if (PublicUtil.isNotEmpty(obrvo.getRoomNo()) && PublicUtil.isNotEmpty(obrvo.getBedNo())) {
                        csunSleepRealDataVo.setLocation(obrvo.getRoomNo() + "-" + obrvo.getBedNo());
                    } else {
                        csunSleepRealDataVo.setLocation("--");
                    }
                }
                // 绑定床位, 老人信息和位置根据床位获取
                if (csunSleepRealDataVo.getBindType().equals(DmcCsunSleepBindTypeEnum.BED.getKey())) {
                    csunSleepRealDataVo.setOldmanName(obrvo.getOldmanName());
                    if (PublicUtil.isNotEmpty(obrvo.getRoomNo()) && PublicUtil.isNotEmpty(obrvo.getBedNo())) {
                        csunSleepRealDataVo.setLocation(obrvo.getRoomNo() + "-" + obrvo.getBedNo());
                    } else {
                        csunSleepRealDataVo.setLocation("--");
                    }
                    csunSleepRealDataVo.setOldmanId(obrvo.getOldmanId());
                }
            }
        }

      //  return WrapMapper.ok(csunSleepRealDataVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),csunSleepRealDataVo);
    }

    /**
     * 查询日创床垫历史数据
     *
     * @param csunSleepDataHiQueryDto   csun sleep data hi query dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/queryCsunSleepHiDataListWithPage", method = RequestMethod.POST)
    public Wrapper queryCsunSleepHiDataListWithPage(@RequestBody CsunSleepDataHiQueryDto csunSleepDataHiQueryDto) {

        Date start = new Date();

        String resultJson = dmcCsunSleepDataHiService.queryCsunSleepHiDataListWithPage(csunSleepDataHiQueryDto);

        Map resultMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(resultJson)) {
            resultMap = (Map) JSON.parseObject(resultJson);
        }

        Date end = new Date();

        log.info("query csun sleep hi data list with page time consuming: {}", (end.getTime() - start.getTime()) + " millisecond");

      //  return WrapMapper.ok(resultMap);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),resultMap);
    }

    /**
     *
     *
     * @param message
     * @return
     */
    @RequestMapping(value = "/sendToMqtt", method = RequestMethod.POST)
    public Wrapper sendToMqtt(String topic, Integer qos, String message) {

        sleepMessageGateway.sendToMqtt(topic, qos, message);

       // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

}
